package com.woniuxy.dudugua.service.impl;

import com.woniuxy.dudugua.entity.Cart;
import com.woniuxy.dudugua.entity.CartExample;
import com.woniuxy.dudugua.mapper.CartMapper;
import com.woniuxy.dudugua.service.CartService;
import com.woniuxy.dudugua.util.view.CartView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceimpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Override
    public List<CartView> getCartView(int userid) {
        List<CartView> cartView = cartMapper.getCartView(userid);
        return cartView;
    }

    @Override
    public int deleteCart(int cartId) {
        int i = cartMapper.deleteByPrimaryKey(cartId);
        return i;
    }

    @Override
    public int deleteUserCart(int userId) {
        CartExample e = new CartExample();
        CartExample.Criteria c = e.createCriteria();
        c.andUserIdEqualTo(userId);
        e.setOrderByClause("id desc");
        int i = cartMapper.deleteByExample(e);
        return i;
    }

    @Override
    public int updateNumber(int id, int number) {
        Cart cart = cartMapper.selectByPrimaryKey(id);
        cart.setNumber(number);
        int i = cartMapper.updateByPrimaryKey(cart);
        return i;
    }

    @Override
    public int insertCart(Cart cart) {
        int i = cartMapper.insert(cart);
        return i;
    }

    @Override
    public Cart selectCart(Integer productId, Integer userId) {
        return cartMapper.selectCart(productId, userId);
    }

    @Override
    public int updateCart(Cart cart) {
        return cartMapper.updateByPrimaryKey(cart);
    }


}
